﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Net;
using System.Xml.Linq;


namespace mashArmory
{
    public class Character
    {
        public String Name { get; set; }
        public int Level { get; set; }
        public int AchPoints { get; set; }
        public String ClassName { get; set; }
        private List<int> _CharClassId;
        public List<int> CharClassId
        {
            get
            {
                if (_CharClassId == null)
                    _CharClassId = new List<int>();
                return _CharClassId;
            }
            set
            {
                _CharClassId = value;
            }

        }
        public String RaceName { get; set; }
        private List<int> _CharRaceId;
        public List<int> CharRaceId
        {

            get
            {
                if (_CharRaceId == null)
                    _CharRaceId = new List<int>();
                return _CharRaceId;
            }

            set
            {
                _CharRaceId = value;
            }
        }
        public int GenderId { get; set; }
        public String CharUrl { get; set; }
        public int GuildRank { get; set; }
    }
    public class Guild
    {
        public int FactionId { get; set; }
        public String GuildName { get; set; }
        public int GuildLevel { get; set; }
        public String Battlegroup { get; set; }
        public int MemberCount { get; set; }
        private List<Character> _Character;
        public List<Character> Character
        {
            get
            {
                if (_Character == null)
                    _Character = new List<Character>();
                return _Character;
            }
            set
            {
                _Character = value;
            }
        }
    }
    public class mshMember
    {
        public Guild GetGuild(string guildName, string realmName)
        {
            mshXML xmlCall = new mshXML();
            XDocument doc = new XDocument();
            doc = xmlCall.GetGuildData(realmName, guildName);
            Guild _Gilde = new Guild();
            List<Character> CharCol = new List<Character>();
            Character singleChar = new Character();

            try
            {
                var counts = from count in doc.Descendants("guildHeader").Attributes("count")
                             select count;
                var factions = from faction in doc.Descendants("guildHeader").Attributes("faction")
                               select faction;
                var BattleGroups = from battleGroup in doc.Descendants("guildHeader").Attributes("battleGroup")
                                   select battleGroup;
                var CharNames = from CharName in doc.Descendants("character").Attributes("name")
                                select CharName;
                var avPoints = from avPoint in doc.Descendants("character").Attributes("achPoints")
                               select avPoint;
                var CharUrls = from CharUrl in doc.Descendants("characte").Attributes("url")
                               select CharUrl;
                var CharLevels = from CharLevel in doc.Descendants("character").Attributes("level")
                                 select CharLevel;
                var ClassIds = from ClassId in doc.Descendants("character").Attributes("classId")
                               select ClassId;
                var GenderIds = from GenderId in doc.Descendants("character").Attributes("genderId")
                                select GenderId;
                var GildenRanks = from GildenRank in doc.Descendants("character").Attributes("rank")
                                  select GildenRank;
                var RaceIds = from RaceId in doc.Descendants("character").Attributes("raceId")
                              select RaceId;

                foreach (int i in counts)
                {
                    _Gilde.MemberCount = i;
                }
                for (int j = 0; j < _Gilde.MemberCount; j++)
                {
                    foreach (int i in RaceIds)
                    {
                        if (i == 1)
                            singleChar.RaceName = "Mensch";
                        else if (i == 11)
                            singleChar.RaceName = "Draenei";
                        else if (i == 22)
                            singleChar.RaceName = "Worgen";
                        else if (i == 3)
                            singleChar.RaceName = "Zwerg";
                        else if (i == 7)
                            singleChar.RaceName = "Gnom";
                        else if (i == 4)
                            singleChar.RaceName = "Nachtelf";
                        else
                            singleChar.RaceName = "Unbekannt";
                        singleChar.CharRaceId.Add(i);
                        CharCol.Add(singleChar);
                    }

                    foreach (int i in GildenRanks)
                    {
                        singleChar.GuildRank = i;
                        CharCol.Add(singleChar);
                    }

                    foreach (int i in GenderIds)
                    {
                        singleChar.GenderId = i;
                        CharCol.Add(singleChar);
                    }

                    foreach (int i in ClassIds)
                    {
                        if (i == 1)
                            singleChar.ClassName = "Krieger";
                        else if (i == 2)
                            singleChar.ClassName = "Paladin";
                        else if (i == 3)
                            singleChar.ClassName = "Jäger";
                        else if (i == 4)
                            singleChar.ClassName = "Schurke";
                        else if (i == 5)
                            singleChar.ClassName = "Priester";
                        else if (i == 6)
                            singleChar.ClassName = "Todesritter";
                        else if (i == 7)
                            singleChar.ClassName = "Schamane";
                        else if (i == 8)
                            singleChar.ClassName = "Magier";
                        else if (i == 9)
                            singleChar.ClassName = "Hexenmeister";
                        else if (i == 11)
                            singleChar.ClassName = "Druide";
                        else
                            singleChar.ClassName = "Unbekannt";
                        singleChar.CharClassId.Add(i);
                        CharCol.Add(singleChar);
                    }

                    foreach (int i in CharLevels)
                    {
                        singleChar.Level = i;
                        CharCol.Add(singleChar);
                    }

                    foreach (string s in CharUrls)
                    {
                        singleChar.CharUrl = s;
                        CharCol.Add(singleChar);
                    }

                    foreach (int i in avPoints)
                    {
                        singleChar.AchPoints = i;
                        CharCol.Add(singleChar);
                    }

                    foreach (String s in BattleGroups)
                    {
                        _Gilde.Battlegroup = s;
                    }

                    foreach (int i in factions)
                    {
                        _Gilde.FactionId = i;
                    }

                    foreach (String s in CharNames)
                    {
                        singleChar.ClassName = s;
                        CharCol.Add(singleChar);
                    }
                }
            }
            catch (XmlException ex)
            {
                throw ex;
            }

            _Gilde.Character = CharCol;

            return _Gilde;
        }
    }
}
